package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyPerformance;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWage;
import cn.stylefeng.guns.sys.modular.system.entity.Formance;
import cn.stylefeng.guns.sys.modular.system.model.params.FormanceParam;
import cn.stylefeng.guns.sys.modular.system.service.BtyPerformanceService;
import cn.stylefeng.guns.sys.modular.system.service.FormanceService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 绩效控制器
 *
 * @author liuy
 * @Date 2021-04-16 09:41:30
 */
@Controller
@RequestMapping("/formance")
public class FormanceController extends BaseController {

    private String PREFIX = "/modular/system/formance";

    @Autowired
    private FormanceService formanceService;
    @Autowired
    BtyPerformanceService performanceService;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2021-04-16
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/formance.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2021-04-16
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/formance_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2021-04-16
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/formance_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2021-04-16
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FormanceParam formanceParam) {
        this.formanceService.add(formanceParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2021-04-16
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FormanceParam formanceParam) {
        this.formanceService.update(formanceParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2021-04-16
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FormanceParam formanceParam) {
        this.formanceService.delete(formanceParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2021-04-16
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FormanceParam formanceParam) {
        Formance detail = this.formanceService.getById(formanceParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2021-04-16
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FormanceParam formanceParam) {
        return this.formanceService.findPageBySpec(formanceParam);
    }

    /**
     * 导出excel列表
     * @author liuy
     * @Date 2021-04-16
     */
    @ResponseBody
    @RequestMapping("/listExport")
    public Object listExport(FormanceParam formanceParam) {
        List<Map<String,Object>> list=this.formanceService.customMapList(formanceParam);
        return list;
    }

    /**
     * 批量审核确认
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmOk")
    @ResponseBody
    public Object confirmOk(@RequestParam String ids) {
//        String[] idss = ids.split(",");
        String[] idss = ids.split(";");
        for(String str : idss){
            String id = str.split(",")[0];
            String sta = str.split(",")[1];
            if (sta.equals("1")){//员工绩效
                Formance formance=formanceService.getById(id);
                String bz=formance.getBz()==null?"":formance.getBz();
                String remark=bz+"9";
                formance.setBz(remark);
                formanceService.updateById(formance);

            }else if (sta.equals("2")){//店主及以上职位绩效
                BtyPerformance performance = performanceService.getById(id);
                String bz = performance.getBz() == null ? "" : performance.getBz();
                String remark = bz + "9";
                performance.setBz(remark);
                performanceService.updateById(performance);
            }

        }
        return SUCCESS_TIP;
    }

    /**
     * 批量取消审核
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmQx")
    @ResponseBody
    public Object confirmQx(@RequestParam String ids) {
        String[] idss = ids.split(",");
        for(String id:idss){
            Formance formance=formanceService.getById(id);
            String bz=formance.getBz()==null?"":formance.getBz();
            String remark="";
            if(bz.contains("9")){
                bz=bz.substring(0,bz.indexOf("9"));
                remark=bz;
            }
            formance.setBz(remark);
            formanceService.updateById(formance);
        }
        return SUCCESS_TIP;
    }

}


